package binance.model

import java.text.*

class Buy {
    Double price
    Long count
    Double target
    Stock stock
    User user
    static hasMany = [sells:Sell]
    static belongsTo = User

    static def currentInvestments(user,sort="stock",order="desc") {
        def result = null
            result = Buy.findAllByUser(user)
        if (sort == "stock")
            result = result.sort{ a,b -> a.stock.name.compareTo(b.stock.name) }
        else if (sort == "progress") {
            result = result.sort{ a,b -> a.progressInteger().compareTo(b.progressInteger()) }
        } else if (sort == "value") {
            result = result.sort{ a,b -> a.valueInteger().compareTo(b.valueInteger()) }
        } else if (sort == "targetValue") {
            result = result.sort{ a,b -> a.targetValueInteger().compareTo(b.targetValueInteger()) }
        }
            if (order == "asc")
                result = result.reverse()

        result = result.findAll{ it.stocksSold() < it.getCount() }
        result
    }

    static Integer sumInvested(user) {
        def result = 0
        Buy.currentInvestments(user).each {
            result += it.price*it.count
        }
        return result
    }

    static Integer sumValue(user) {
        def result = 0
        Buy.currentInvestments(user).each {
            result += it.stock.value*it.count*it.stock.currency.sek
        }
        return result
    }

    static Integer sumProfit(user) {
        def result = 0
        return sumValue(user)-sumInvested(user)
    }

    static Integer sumTarget(user) {
        def result = 0
        Buy.currentInvestments(user).each {
            result += it.target*it.count
        }
        return result
    }

    static Integer avgProfitProc(user) {
        def invested = sumInvested(user);
        if (invested == 0) {
            return 0;
        }
        return (sumValue(user)/invested-1)*100
    }

    Long stocksSold() {
        int num = 0;
        sells.each {
            num += it.getCount()
        }
        return num
    }

    Long stocksInvested() {
        return count-stocksSold()
    }

    String toString() {
        return stock.toString() + ", " + price + "kr/stock"
    }

    String profit() {
        def rounded = count*(stock.value*stock.currency.sek-price)
        DecimalFormat df = new DecimalFormat("0.00");
        def formatted =  df.format(rounded);
        return formatted
    }

    String profitProc() {
        def rounded = (stock.value*stock.currency.sek/price-1)*100
        DecimalFormat df = new DecimalFormat("0.00");
        rounded = df.format(rounded);
        return rounded
    }

    String progress() {
        def rounded = progressInteger()
        DecimalFormat df = new DecimalFormat("0.00");
        rounded = df.format(rounded);
        return rounded
    }

    Integer progressInteger() {
        def rounded = (stock.value*stock.currency.sek-price)/((target-price)/100)
        return rounded
    }

    String invested() {
        def rounded = price*count
        DecimalFormat df = new DecimalFormat("### ### ##0.00");
        def formatted = df.format(rounded);
        return formatted
    }

    String value() {
        def rounded = valueInteger()
        DecimalFormat df = new DecimalFormat("0.00");
        rounded = df.format(rounded);
        return rounded
    }

    Integer valueInteger() {
        stock.value*stock.currency.sek*count
    }

    String targetValue() {
        def rounded = targetValueInteger()
        DecimalFormat df = new DecimalFormat("0.00");
        rounded = df.format(rounded);
        return rounded
    }

    Integer targetValueInteger() {
        target*count
    }
}
